# -*- coding:utf-8 -*-
__author__ = 'LookCheng'
from . import db

from datetime import datetime

tags = db.Table('tags',
                db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
                db.Column('joke_id', db.Integer, db.ForeignKey('joke.id'))
                )

class Joke(db.Model):
    __tablename__ = 'joke'
    id = db.Column(db.Integer, primary_key=True)
    tags = db.relationship('Tag', secondary=tags,
                           backref=db.backref('jokes', lazy='dynamic'))
    body = db.Column(db.Text)
    nums = db.Column(db.Integer)
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)
    likes = db.Column(db.Integer)
    is_deleted =db.Column(db.Boolean,default=False)

    def __init__(self, body, nums=None, timestamp=None, likes=0, is_deleted=False):
        self.body = body
        if timestamp is None:
            timestamp = datetime.utcnow()
        self.timestamp = timestamp
        self.likes = likes
        self.is_deleted = is_deleted
        if nums is None:
            nums = len(body)
        self.nums = nums

    def __repr__(self):
        return '<User %r>' % self.username


class Tag(db.Model):
    __tablename__ = 'tag'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64))
